home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BBS in a Box 7
/
BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso
/
Files
/
DA
/
P
/
PCalculator.cpt
/
CalcDA sources
/
CalcDA_Buttons.c
< prev
next >
Wrap
Text File
|
1990-07-06
|
12KB
|
362 lines
/*
* CalcDA_Buttons.c for the Programmer's Calculator project
*
* Copyright 1990, Peter Ohler
*
* All Rights Reserved
*
* The Programmer's Calculator and the source code are shareware. That means
* they are not free. If you use either the source or the calculator then
* send $5 or $10 (whatever you feel its worth) to the address that follows.
* The source and calculator can be distributed for free. Prior to any sale
* of either the source code or the calculator my permission must be
* obtained. This includes sales by shareware distribution houses that sell
* shareware.
*
* Peter Ohler
*
* 3184 Rohrer Drive, Lafayette CA 94549
*
* (415) 284-7828
*
* ***************************************************************************
*
* This file contains all the button definitions, including the button names,
* the key key to button mapping, and the functions to execute for each
* button. Ideally (and initially) all the button names and functions should
* be in on C structure. Unfortunately it is not possible to initialize
* pointers to functions at compile time for a DA. Instead an ugly switch
* statement is used to execute the functions for each button and the button
* names are kept in an array. At least it works.
*/
#include <SANE.h>
#include "CalcDA.h"
/*
* ***************************************************************************
* prototypes
*/
extern void UpdateXRegText(void);
extern int ToggleRegs(void);
extern int ToggleStack(Stack *stack);
extern int DoIntDigit(int digit);
extern int DoDoubleDigit(int digit);
extern int DelIntDigit(void);
extern int DelDoubleDigit(void);
extern int ClearAllReg(void);
extern int ClearXReg(void);
extern int CopyXreg(void);
extern int CopyToXreg(void);
extern int SwapXreg(void);
extern int AddX2Reg(void);
extern int ChangeSign(void);
extern void FreezeXReg(void);
extern double Power(double x, double y);
extern int DoArithmetic(IntFunc func);
extern int DoEqual(void);
extern int DoOr(void);
extern int DoAnd(void);
extern int DoXor(void);
extern int DoShiftRight(void);
extern int DoShiftLeft(void);
extern int DoPlus(void);
extern int DoMinus(void);
extern int DoMultiply(void);
extern int DoDivide(void);
extern int DoModulo(void);
extern int DoPower(void);
extern int DoInvPower(void);
extern double Log10(double x);
extern int DoXFactorial(void);
extern int DoTrig(DoubleFunc func, int inv);
extern double Sine(double x);
extern double Cosine(double x);
extern double Tangent(double x);
extern double ASin(double x);
extern double ACos(double x);
extern double ArcTangent(double x);
extern int DoAscii(void);
extern int DoPrecision(void);
int DoButton(int n);
/*
* ***************************************************************************
* variables
*/
extern double OneEighty;
extern double One;
extern int exponentActive;
extern int afterDigits;
double PI = 3.14159265358979323846;
/*
* The key map table for the integer mode of the calculator. A value of 255
* indicates there is no mapping. Other values are the button numbers. A
* button number is (row * number of columns + column) * 2 + (1 if in the
* double mode, 0 if in the integer mode).
*/
unsigned char iKeyButtonMap[256] = {
255, 255, 255, 255, 255, 255, 255, 255, /* 0 */
46, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 16 */
255, 255, 255, 5, 255, 255, 255, 255,
255, 255, 255, 255, 255, 33, 11, 255, /* 32 */
255, 255, 31, 38, 255, 39, 255, 32,
42, 35, 36, 37, 28, 29, 30, 21, /* 48 */
22, 23, 255, 255, 255, 48, 255, 255,
255, 14, 15, 16, 7, 8, 9, 255, /* 64 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 80 */
255, 255, 255, 255, 255, 255, 12, 255,
255, 14, 15, 16, 7, 8, 9, 255, /* 96 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 112 */
255, 255, 255, 255, 10, 255, 19, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 128 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 144 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 160 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 40, 255, 255, 255, 255, 255, 255, /* 176 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 192 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 208 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 224 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 240 */
255, 255, 255, 255, 255, 255, 255, 255
};
/*
* The real or double mode keymap
*/
unsigned char rKeyButtonMap[256] = {
255, 255, 255, 255, 255, 255, 255, 255, /* 0 */
46, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 16 */
255, 255, 255, 5, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 32 */
255, 255, 31, 38, 255, 39, 43, 32,
42, 35, 36, 37, 28, 29, 30, 21, /* 48 */
22, 23, 255, 255, 255, 48, 255, 255,
255, 255, 255, 255, 255, 44, 255, 255, /* 64 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 80 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 44, 255, 255, /* 96 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 112 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 128 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 144 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 160 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 40, 255, 255, 255, 255, 255, 255, /* 176 */
45, 45, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 192 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 208 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 224 */
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, /* 240 */
255, 255, 255, 255, 255, 255, 255, 255
};
/*
* These are all the button names. The button number or index is the same as
* the key map arrays.
*/
char buttonNames[98][6] = { "\pReg", "\pReg",
"\pType", "\pType",
"\pBase", "\pNote",
"\pSign", "\prad/°",
"\pASCII", "\pPrec",
"\pClr", "\pClr",
"\pClrx", "\pClrx",
"\pD", "\psin",
"\pE", "\pcos",
"\pF", "\ptan",
"\p|", "\p°>rad",
"\p&", "\p10^x",
"\p^", "\plog",
"\px^2", "\px^2",
"\pA", "\pasin",
"\pB", "\pacos",
"\pC", "\patan",
"\p<<", "\prad>°",
"\p>>", "\pe^x",
"\p~", "\pln",
"\p√x", "\p√x",
"\p7", "\p7",
"\p8", "\p8",
"\p9", "\p9",
"\px>>R", "\px>>R",
"\px<<R", "\px<<R",
"\px<>R", "\px<>R",
"\py^x", "\py^x",
"\p4", "\p4",
"\p5", "\p5",
"\p6", "\p6",
"\p*", "\p*",
"\p÷", "\p÷",
"\p%", "\p1/x",
"\px√y", "\px√y",
"\p1", "\p1",
"\p2", "\p2",
"\p3", "\p3",
"\p+", "\p+",
"\p-", "\p-",
"\p±", "\p±",
"\px!", "\px!",
"\p0", "\p0",
"\pFF", "\p.",
"\pFFFF", "\pexp",
"\pFFF…", "\pπ",
"\pdel", "\pdel",
"\pR+=x", "\pR+=x",
"\p=", "\p="
};
/*
* ***************************************************************************
* functions
*/
/*
* Just one large switch statement. All functions return 0 if successful and
* non zero if an error occurs. The return value of the function is the
* result code of the button function executed.
*/
int
DoButton(n)
int n;
{
int ok = 0;
double *dx = &xReg.value.dv;
long *lx = &xReg.value.lv;
switch (n * 2 + typeStack.pos) {
case 0:
case 1: ok = ToggleRegs(); break;
case 2:
case 3: ok = ToggleStack(&typeStack); break;
case 4: ok = ToggleStack(&baseStack); break;
case 5: ok = ToggleStack(¬eStack); break;
case 6: ok = ToggleStack(&signStack); break;
case 7: ok = ToggleStack(&angleStack); break;
case 8: ok = DoAscii(); break;
case 9: ok = DoPrecision(); break;
case 10:
case 11: ok = ClearAllReg(); break;
case 12:
case 13: ok = ClearXReg(); break;
case 14: ok = DoIntDigit(0x0D); break;
case 15: ok = DoTrig(Sine, 0); break;
case 16: ok = DoIntDigit(0x0E); break;
case 17: ok = DoTrig(Cosine, 0); break;
case 18: ok = DoIntDigit(0x0F); break;
case 19: ok = DoTrig(Tangent, 0); break;
case 20: ok = DoArithmetic(DoOr); break;
case 21: *dx *= PI / OneEighty; FreezeXReg(); break;
case 22: ok = DoArithmetic(DoAnd); break;
case 23: *dx = Power(10.0, *dx); FreezeXReg(); break;
case 24: ok = DoArithmetic(DoXor); break;
case 25: *dx = Log10(*dx); FreezeXReg(); break;
case 26: *lx *= *lx; FreezeXReg(); break;
case 27: *dx *= *dx; FreezeXReg(); break;
case 28: ok = DoIntDigit(0x0A); break;
case 29: ok = DoTrig(ASin, 1); break;
case 30: ok = DoIntDigit(0x0B); break;
case 31: ok = DoTrig(ACos, 1); break;
case 32: ok = DoIntDigit(0x0C); break;
case 33: ok = DoTrig(ArcTangent, 1); break;
case 34: ok = DoArithmetic(DoShiftLeft); break;
case 35: *dx *= OneEighty / PI; FreezeXReg(); break;
case 36: ok = DoArithmetic(DoShiftRight); break;
case 37: *dx = Power(2.7182818284, *dx); FreezeXReg(); break;
case 38: *lx = ~*lx; FreezeXReg(); break;
case 39: *dx = log(*dx); FreezeXReg(); break;
case 40: *lx = sqrt((double)*lx); FreezeXReg(); break;
case 41: *dx = sqrt(*dx); FreezeXReg(); break;
case 42: ok = DoIntDigit(7); break;
case 43: ok = DoDoubleDigit(7); break;
case 44: ok = DoIntDigit(8); break;
case 45: ok = DoDoubleDigit(8); break;
case 46: ok = DoIntDigit(9); break;
case 47: ok = DoDoubleDigit(9); break;
case 48:
case 49: ok = CopyXreg(); break;
case 50:
case 51: ok = CopyToXreg(); break;
case 52:
case 53: ok = SwapXreg(); break;
case 54:
case 55: ok = DoArithmetic(DoPower); break;
case 56: ok = DoIntDigit(4); break;
case 57: ok = DoDoubleDigit(4); break;
case 58: ok = DoIntDigit(5); break;
case 59: ok = DoDoubleDigit(5); break;
case 60: ok = DoIntDigit(6); break;
case 61: ok = DoDoubleDigit(6); break;
case 62:
case 63: ok = DoArithmetic(DoMultiply); break;
case 64:
case 65: ok = DoArithmetic(DoDivide); break;
case 66: ok = DoArithmetic(DoModulo); break;
case 67: *dx = One / *dx; FreezeXReg(); break;
case 68:
case 69: ok = DoArithmetic(DoInvPower); break;
case 70: ok = DoIntDigit(1); break;
case 71: ok = DoDoubleDigit(1); break;
case 72: ok = DoIntDigit(2); break;
case 73: ok = DoDoubleDigit(2); break;
case 74: ok = DoIntDigit(3); break;
case 75: ok = DoDoubleDigit(3); break;
case 76:
case 77: ok = DoArithmetic(DoPlus); break;
case 78:
case 79: ok = DoArithmetic(DoMinus); break;
case 80:
case 81: ok = ChangeSign(); break;
case 82:
case 83: ok = DoXFactorial(); break;
case 84: ok = DoIntDigit(0); break;
case 85: ok = DoDoubleDigit(0); break;
case 86: *lx = 0xFFL; FreezeXReg(); break;
case 87: afterDigits = 1; UpdateXRegText(); break;
case 88: *lx = 0xFFFFL; FreezeXReg(); break;
case 89: exponentActive = 1; UpdateXRegText(); break;
case 90: *lx = 0xFFFFFFFFL; FreezeXReg(); break;
case 91: *dx = PI; FreezeXReg(); break;
case 92: ok = DelIntDigit(); break;
case 93: ok = DelDoubleDigit(); break;
case 94:
case 95: ok =AddX2Reg(); break;
case 96:
case 97: ok = DoArithmetic(DoEqual); break;
default: ok = -1; break;
}
return ok;
};